package pe.edu.upc.dew.grupo04.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.support.WebApplicationContextUtils;

import pe.edu.upc.dew.grupo04.dao.PeliculaDao;
import pe.edu.upc.dew.grupo04.model.Pelicula;
import pe.edu.upc.dew.grupo04.service.PeliculaService;

/**
 * Servlet implementation class PeliculaServlet
 */
public class PeliculaServlet extends HttpServlet implements PeliculaService {
	private static final long serialVersionUID = 1L;

	private PeliculaService peliculaService;

	@Override
	public void init(ServletConfig config) throws ServletException {
		// Integracion de Servlets con Spring
		this.peliculaService = WebApplicationContextUtils
				.getWebApplicationContext(config.getServletContext()).getBean(
						"peliculaService", PeliculaService.class);
	}

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PeliculaServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see PeliculaService#crearPelicula(Pelicula)
	 */
	public void crearPelicula(Pelicula pelicula) {
		// TODO Auto-generated method stub
	}

	/**
	 * @see PeliculaService#setPeliculaDao(PeliculaDao)
	 */
	public void setPeliculaDao(PeliculaDao peliculaDao) {
		// TODO Auto-generated method stub
	}

	/**
	 * @see PeliculaService#getPeliculasEnCartelera()
	 */
	public List<Pelicula> getPeliculasEnCartelera() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @see PeliculaService#getPeliculaPorCodigo(int)
	 */
	public Pelicula getPeliculaPorCodigo(int codPelicula) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @see PeliculaService#updatePelicula(Pelicula)
	 */
	public void updatePelicula(Pelicula Pelicula) {
		// TODO Auto-generated method stub
	}

	/**
	 * @see PeliculaService#eliminarPelicula(int)
	 */
	public void eliminarPelicula(int codPelicula) {
		// TODO Auto-generated method stub
	}

	/**
	 * @see PeliculaService#getPeliculasEnEstreno()
	 */
	public List<Pelicula> getPeliculasEnEstreno() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @see PeliculaService#getPeliculas()
	 */
	public List<Pelicula> getPeliculas() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {

			Pelicula pelicula = new Pelicula();
			pelicula.setCodPelicula(Integer.parseInt(request.getParameter("codPelicula")));
			pelicula.setTitulo(request.getParameter("titulo"));
			pelicula.setCartelera(Boolean.parseBoolean(request.getParameter("cartelera")));
			pelicula.setEstreno(Boolean.parseBoolean(request.getParameter("estreno")));
			pelicula.setDuracion(Integer.parseInt(request.getParameter("duracion")));
			pelicula.setGenero(request.getParameter("genero"));
			pelicula.setClasificacion(request.getParameter("clasificacion"));
			
			peliculaService.crearPelicula(pelicula);
			request.getRequestDispatcher("pelicula.jsp").forward(request,
					response);
		} catch (Exception e) {
			String Cadena = " Existieron algunos Errores : Error" + e.getMessage();
			request.setAttribute("Cadena", Cadena);				
			request.getRequestDispatcher("pelicula.jsp").forward(request,
					response);
		}

	}

}
